package com.example.zyfypt516wh.model;

import com.example.zyfypt516wh.service.FocusService;
import com.example.zyfypt516wh.bean.FocusResult;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.FocusIface;
import com.example.zyfypt516wh.iface.FocusListener;
import com.example.zyfypt516wh.iface.ResListener;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FocusModel implements FocusIface {
    private Retrofit retrofit;
    public FocusModel() {
        //使用 Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL).addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void exist(String mod, int idolid, String sessionID, FocusListener listener) {
        //使用 Retrofit----2
        FocusService service=retrofit.create(FocusService.class);
        Call<String> call =service.exists(mod,idolid,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void focus(String mod, int idolid, String sessionID, FocusListener listener) {

        //使用 Retrofit----2
        FocusService service=retrofit.create(FocusService.class);
        Call<String> call =service.focus(mod,idolid,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void unfocus(String mod, int idolid, String sessionID, FocusListener listener) {

        //使用 Retrofit----2
        FocusService service=retrofit.create(FocusService.class);
        Call<String> call =service.unfocus(mod,idolid,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }

    /**
     * 获取我关注的用户列表
     * @param mod
     * @param page
     * @param sessionID
     * @param listener
     */
    @Override
    public void myFocus(String mod, int page, String sessionID, FocusListener listener) {

        //使用 Retrofit----2
        FocusService service=retrofit.create(FocusService.class);
        Call<List<FocusResult>> call =service.getFocusUserList(mod,page,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<List<FocusResult>>() {
            @Override
            public void onResponse(Call<List<FocusResult>> call, Response<List<FocusResult>> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<List<FocusResult>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    /**
     * 获取关注用户的关注列表
     * @param mod
     * @param page
     * @param sessionID
     * @param userid
     * @param listener
     */
    @Override
    public void FocusList(String mod, int page, String sessionID, int userid, ResListener listener) {
        //使用 Retrofit----2
        FocusService service=retrofit.create(FocusService.class);
        Call<List<ResBean>> call =service.getFollowList(mod,page,sessionID,userid);
        //使用 Retrofit----3
        call.enqueue(new Callback<List<ResBean>>() {
            @Override
            public void onResponse(Call<List<ResBean>> call, Response<List<ResBean>> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<List<ResBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
